package com.bigdata.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bigdata.common.utils.StringUtils;
import com.bigdata.common.core.page.TableDataInfo;
import com.bigdata.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.bigdata.service.domain.bo.DatasetTagsBo;
import com.bigdata.service.domain.vo.DatasetTagsVo;
import com.bigdata.service.domain.DatasetTags;
import com.bigdata.service.mapper.DatasetTagsMapper;
import com.bigdata.service.service.IDatasetTagsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 数据集-标签关联Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-22
 */
@RequiredArgsConstructor
@Service
public class DatasetTagsServiceImpl implements IDatasetTagsService {

    private final DatasetTagsMapper baseMapper;

    /**
     * 查询数据集-标签关联
     */
    @Override
    public DatasetTagsVo queryById(String datasetId){
        return baseMapper.selectVoById(datasetId);
    }

    /**
     * 查询数据集-标签关联列表
     */
    @Override
    public TableDataInfo<DatasetTagsVo> queryPageList(DatasetTagsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DatasetTags> lqw = buildQueryWrapper(bo);
        Page<DatasetTagsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询数据集-标签关联列表
     */
    @Override
    public List<DatasetTagsVo> queryList(DatasetTagsBo bo) {
        LambdaQueryWrapper<DatasetTags> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<DatasetTags> buildQueryWrapper(DatasetTagsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DatasetTags> lqw = Wrappers.lambdaQuery();
        return lqw;
    }

    /**
     * 新增数据集-标签关联
     */
    @Override
    public Boolean insertByBo(DatasetTagsBo bo) {
        DatasetTags add = BeanUtil.toBean(bo, DatasetTags.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改数据集-标签关联
     */
    @Override
    public Boolean updateByBo(DatasetTagsBo bo) {
        DatasetTags update = BeanUtil.toBean(bo, DatasetTags.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(DatasetTags entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除数据集-标签关联
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 删除数据集标签
     */
    @Override
    public Boolean deleteByDatasetId(Long datasetId) {
        return baseMapper.delete(Wrappers.lambdaQuery(DatasetTags.class).eq(DatasetTags::getDatasetId, datasetId)) > 0;
    }
}
